package com.health.viewscontrols.focusgroup;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import com.health.R;
import com.health.adapter.FragmentAdapter;
import com.health.fragment.focusgroup.FeiXinBingFragment;
import com.health.fragment.focusgroup.TNB_SfFragment;
import com.health.tools.GlobalConts;
import com.health.viewscontrols.NovaFragmentActivity;
import com.health.viewscontrols.PersonalFileActivity;
import com.novabluetooth.views.ExamHistoryActivity;

import java.util.ArrayList;

/**肺心病
 * Created by John on 2019/7/29.
 */

public class FeiXinBingActivity extends NovaFragmentActivity {
    public static final int ResultXueya = 1;
    private ViewPager viewPager;
    private ArrayList<RadioButton> radioButtons;
    private ArrayList<Fragment> fragments;

    private TextView title;
    private Button btn_back;
    private  Button btn_up;
    private FragmentAdapter adapter;
    public static Activity feiXinBingActivity = null;
    RadioButton maternity_base;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.gxy_main);
        initView();
        addListener();
    }

    public void initView(){
        title = (TextView) findViewById(R.id.title);

        btn_back = (Button) findViewById(R.id.btn_back);
        btn_up = (Button) findViewById(R.id.btn_up);

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        maternity_base = (RadioButton) findViewById(R.id.sf_info_base);

        radioButtons = new ArrayList<RadioButton>();
        radioButtons.add(maternity_base);


        Fragment baseFragement = new FeiXinBingFragment();
        fragments = new ArrayList<Fragment>();
        fragments.add(baseFragement);

        adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        title.setText("肺心病 —— 随访信息");
        setChecked(0);
        feiXinBingActivity = this;
    }
    public void addListener(){
        maternity_base.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
                setChecked(0);
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                radioButtons.get(position).setChecked(true);
                setChecked(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btn_up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonalFileActivity.onFragmentSave currentFragment = (PersonalFileActivity.onFragmentSave) adapter.getCurrentFragment();
                currentFragment.save();
            }
        });
    }

  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        PersonalFileActivity.onFragmentSave fragment = (PersonalFileActivity.onFragmentSave) adapter.getCurrentFragment();
        if (requestCode == GlobalConts.RequestXueya && resultCode == ExamHistoryActivity.ResultXueya) {
            fragment.setXueya(data.getStringExtra("高压"), data.getStringExtra("低压"), data.getStringExtra("脉率"));
        } else if (requestCode == GlobalConts.RequestTizhong && resultCode == ExamHistoryActivity.ResultTizhong) {
            fragment.setTizhong(data.getStringExtra("value"));
        }else if (requestCode == GlobalConts.RequestMaiLv && resultCode == com.novabluetooth.views.ExamHistoryActivity.ResultMaiLv) {
            Log.w("eeeeeeeee", data.getStringExtra("value"));
            fragment.setMaiLv(data.getStringExtra("value"));
        }else if(requestCode == GlobalConts.RequestXueyang && resultCode == ExamHistoryActivity.ResultXueyang){
            fragment.setXueYang(data.getStringExtra("value"));
        }
    }


    @SuppressLint("ResourceAsColor")
    private void setChecked(int pos) {
        for (RadioButton rbtn : radioButtons) {
            rbtn.setBackground(null);
        }
        radioButtons.get(pos).setBackgroundResource(R.drawable.rd_chek_center);
        if (pos == 0) {
            title.setText("肺心病  —— 随访信息");
        }
    }
}
